package com.training.admin.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.demo2do.core.persistence.GenericDaoSupport;
import com.demo2do.core.persistence.annotation.Dao;
import com.demo2do.core.persistence.support.QueryFilter;
import com.demo2do.core.utils.StringUtils;
import com.training.admin.dao.IdmUserDao;
import com.training.admin.dao.TrainerDao;
import com.training.admin.entity.trainee.IdmUser;
import com.training.admin.entity.trainer.Trainer;
import com.training.admin.service.TrainerService;
/**
 * 
 * @author Rockett
 *
 */
@Service("trainerService")
public class TrainerServiceImpl extends DataServiceImpl<Trainer> implements TrainerService{

	@Autowired
	private GenericDaoSupport genericDaoSupport;
	
	@Dao
	private TrainerDao trainerDao;
	
	@Dao
	private IdmUserDao idmUserDao;

	@SuppressWarnings("unchecked")
    @Override
	public List<Trainer> listByName(String name) {
		QueryFilter qf = new QueryFilter().addLike("name", name);
		return  genericDaoSupport.searchForList("FROM Trainer WHERE disabled = false "+ qf.clauseSentence(),qf.parameters());
	}

    /* (non-Javadoc)
     * @see com.training.admin.service.TrainerService#getTrainerIdAndName(java.lang.String)
     */
    public List<Map<String, String>> getTrainerIdAndName(String query) {
        
        String search = StringUtils.connect("%", query, "%");
        List<IdmUser> idmUsers = idmUserDao.listBySearch(search, search, search, search);
        
        List<Map<String, String>> listMap = new ArrayList<>();
        for(IdmUser idmUser : idmUsers) {
            
            String item = idmUser.getUserorgnamefullpath();
            String result ="";
            String [] strings = item.split("-");
            for (int i = 2; i<strings.length; i++) {
                if (i != strings.length-1) {
                    result += strings[i] + "-";
                }else {
                    result += strings[i];
                }
            }
            
            Map<String, String> map = new HashMap<String,String>();
            map.put("id", idmUser.getId().toString());
            map.put("text", idmUser.getUsercn() + "-" + result);
            listMap.add(map);
        }
        
        return listMap;
    }

	/*
	 * (non-Javadoc)
	 * @see com.training.admin.service.TrainerService#loadByuserName(java.lang.String)
	 */
	public List<IdmUser> loadByUserName(String userName) {
		
	  List<IdmUser> idmUsers = trainerDao.loadByUserName(userName);
		
	  return idmUsers;
	}

	/*
	 * (non-Javadoc)
	 * @see com.training.admin.service.TrainerService#getIdmUser(java.lang.String)
	 */
	public List<Map<String, String>> getIdmUser(String userName) {
		String search = StringUtils.connect("%", userName, "%");
		List<IdmUser> idmUsers = trainerDao.loadByUserName(search);
		List<Map<String, String>> maps = new ArrayList<Map<String,String>>();
		for(IdmUser idmUser : idmUsers) {
			Map<String, String> map = new HashMap<String, String>();
			map.put("id", idmUser.getUsercn());
			map.put("text", idmUser.getUsercn());
			map.put("email", idmUser.getMail());
			maps.add(map);
		}
		
		return maps;
	}


	
}
